using GameFramework.Event;
using UnityGameFramework.Runtime;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;
namespace VRLauncher

{
    public class ProcedureHouse : ProcedureBase
    {
        private ProcedureOwner procedureOwner;
        private bool changeScene = false;
        private bool returnMian = false;
        public override bool UseNativeDialog => throw new System.NotImplementedException();

        public void ReturnMian()
        {
            returnMian = true;
        }
        public void ChangerScene(int sceneID)
        {
            procedureOwner.SetData<VarInt32>("NextSceneId", sceneID);
            changeScene = true;
        }

        protected override void OnDestroy(ProcedureOwner procedureOwner)
        {
            base.OnDestroy(procedureOwner);
        }

        protected override void OnEnter(ProcedureOwner procedureOwner)
        {
            base.OnEnter(procedureOwner);
            GameManager.Event.Subscribe(ChangeSceneEventArgs.EventId, OnChangeScene);

            this.procedureOwner = procedureOwner;
            returnMian = false;
            changeScene = false;
            GameManager.UI.OpenUIForm(UIFormId.HouseForm, this);
        }

        protected override void OnInit(ProcedureOwner procedureOwner)
        {
            base.OnInit(procedureOwner);
        }

        protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown)
        {
            base.OnLeave(procedureOwner, isShutdown);
            GameManager.Event.Unsubscribe(ChangeSceneEventArgs.EventId, OnChangeScene);
        }

        protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
            if (changeScene)
            {
                ChangeState<ProcedureChangeScene>(procedureOwner);
            }
            if (returnMian)
            {
                procedureOwner.SetData<VarInt32>("NextSceneId", GameManager.Config.GetInt("Scene.Centre"));
                ChangeState<ProcedureChangeScene>(procedureOwner);
            }
        }


        private void OnChangeScene(object sender, GameEventArgs e)
        {
            ChangeSceneEventArgs ne = (ChangeSceneEventArgs)e;
            if (ne == null)
                return;
            changeScene = true;
            procedureOwner.SetData<VarInt32>("NextSceneId", ne.SceneId);
        }
    }
}
